/**
 * 
 */
package hb_Teach_InitObject_Date;

import java.util.Calendar;
import java.util.Date;

import org.junit.Test;

/**
 * @author Jiang Zhiyan
 *
 * 2021年5月13日 上午9:35:58
 */
public class DateString {
	@Test
	public void dateTest(){
		Date nowTime = new Date();
		System.out.println(nowTime);
	}
	
	/**
	 * Calendar 用法
	 * get(Calendar.) 获取日期信息
	 * calendar.set(y, m-1,d)  修改Calendar中存放的年，月，日的值
	 * calendar.getTimeInMillis();   获取日期年月日，星期 的值
	 * 
	 */
	@Test
	public void calendarTest() {
//		初始化对象
		Calendar calendar = Calendar.getInstance();
//		calendar.get(Calendar.MONTH);
		calendar.setTime(new Date());
		int year = calendar.get(Calendar.YEAR), //get方法能获取到日期的值，参数为Calendar的静态常量
			month = calendar.get(Calendar.MONTH+1),
		    day = calendar.get(Calendar.DAY_OF_MONTH),
		    hour = calendar.get(Calendar.HOUR_OF_DAY),
		    minute = calendar.get(Calendar.MINUTE),
		    second = calendar.get(Calendar.SECOND);
		System.out.println("现在的时间是：");
		System.out.println( year+"年"+month +"月"+day+"日");
		System.out.println(" "+hour+ "时"+ minute +"分"+second+"秒");
		System.out.println("--------");
		int y=2012,m=9,d=1;
		calendar.set(y, m-1,d);  //定义存入一个日期
		long time1= calendar.getTimeInMillis();
		y=2016;
		m=7;
		day=1;
		calendar.set(y,m-1,d);
		long time2= calendar.getTimeInMillis();
		long subDay = (time2-time1)/(1000*60*60*24); //将毫秒转换成天
		System.out.println(""+ new Date(time2));
		System.out.println("与" +new Date(time1));
		System.out.println("相隔"+ subDay +"天");
				}
	
	@Test
	public void formatTest() {
		Date now= new Date();
		String s1= String.format("%tY年%tm月%td日", now,now,now);
		String s2= String.format("%tF", now);
	}
}
